Introduction to FFTs

RJ Nowling

The Fourier Series is given by

$$ s_N(t) = \frac{1}{2}a_0 + \sum_{n=1}^\infty a_n cos(2 \pi f nt) + b_n sin(2 \pi f nt) $$

where $t$ is the time, $f$ is the frequency, and $a_i$ and $b_i$ are amplitudes.

The power of Fourier Series lies in the ability to approximate many functions by selecting appropriate coefficents $a_i$ and $b_i$. In doing so, we get a frequency decomposition of the original signal. The process of determining the coefficients is called a Fourier Transform (FT) and is used extensively in digital signal processing. Common uses of FTs include identifying the frequencies present in signals and filtering or selecting specific frequency ranges.

Here, I'm going to give a practical tutorial about using the fast Fourier Transforms (FFTs) to analyze signals. Conceptually, FFTs are not hard to use -- the difficulties lie in knowing how to use the results and properly convert the units.

Let's start with a simple example where we create a signal containing specific frequencies:

$$ x(t) = A \sin(2 \pi f t + \phi) $$

where $A$ is the amplitude, $f$ is the frequency (given in cycles per unit time), $t$ is the time given in seconds, and $\phi$ is the phase given in seconds.

We'll create 3 signals with frequencies of 1, 2, and 4 Hz, all with amplitudes of 1.


In [10]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

n_samples = 128

# time, in seconds
ts = np.linspace(0., 1., num=n_samples)
x_1 = np.sin(2. * np.pi * 1. * ts)
x_2 = np.sin(2. * np.pi * 2. * ts)
x_4 = np.sin(2. * np.pi * 4. * ts)

plt.plot(ts, x_1, label="1 Hz")
plt.plot(ts, x_2, label="2 Hz")
plt.plot(ts, x_4, label="4 Hz")
plt.plot(ts, x_1 + x_2 + x_4, label="Combined")
plt.legend()
plt.xlim([0., 1.25])
plt.ylim([-3., 3.])
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
Out[10]:
<matplotlib.text.Text at 0x4a59510>

In the above example, the blue line is the 1 Hz signal -- it goes through 1 period per second. The green line is the 2 Hz signal -- it goes through 2 periods per second. The red line is the 4 Hz signal -- it goes through 4 periods per second. The cyan line is the combined signal.

We took 128 samples over 1 second, or 128 Hz. According to the Nyquist theorem, to sample a signal of frequency $f$, we must take samples at a frequency of $2f$. As a result, the fastest signal we can determine from our samples is 64 Hz.

The slowest signal we can determine is determined by the elapsed time covered in our samples. In our case, our sample covered 1 s, so our slowest recoverable signal is 1 Hz.

Knowing the time period (1 s) covered by the samples, the number of samples (128), and the resulting sampling frequency will be important for converting the results of the FFT to the correct units.


In [11]:
elapsed_time = 1.
freq = np.fft.fftfreq(n_samples, d=elapsed_time / n_samples)
print freq

coeff = np.fft.fft(x_1 + x_2 + x_4) / n_samples / 2.

print coeff

amplitudes = np.abs(coeff)

plt.plot(freq[0:n_samples/2], amplitudes[0:n_samples/2])
plt.xlabel("Frequency (Hz)", fontsize=16)
plt.ylabel("Amplitude", fontsize=16)
plt.xlim([0, 20])


[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.
  15.  16.  17.  18.  19.  20.  21.  22.  23.  24.  25.  26.  27.  28.  29.
  30.  31.  32.  33.  34.  35.  36.  37.  38.  39.  40.  41.  42.  43.  44.
  45.  46.  47.  48.  49.  50.  51.  52.  53.  54.  55.  56.  57.  58.  59.
  60.  61.  62.  63. -64. -63. -62. -61. -60. -59. -58. -57. -56. -55. -54.
 -53. -52. -51. -50. -49. -48. -47. -46. -45. -44. -43. -42. -41. -40. -39.
 -38. -37. -36. -35. -34. -33. -32. -31. -30. -29. -28. -27. -26. -25. -24.
 -23. -22. -21. -20. -19. -18. -17. -16. -15. -14. -13. -12. -11. -10.  -9.
  -8.  -7.  -6.  -5.  -4.  -3.  -2.  -1.]
[ -3.91396984e-17 +0.00000000e+00j   6.19890947e-03 -2.52515577e-01j
   1.22100235e-02 -2.48540738e-01j   1.71184207e-05 -2.32068767e-04j
   2.40057698e-02 -2.43734671e-01j  -1.43238147e-03 +1.16134422e-02j
  -1.01943025e-03 +6.87244050e-03j  -8.85687136e-04 +5.10434074e-03j
  -8.20763560e-04 +4.12625706e-03j  -7.83110774e-04 +3.48735064e-03j
  -7.58918476e-04 +3.02977239e-03j  -7.42290393e-04 +2.68229282e-03j
  -7.30297537e-04 +2.40746834e-03j  -7.21327735e-04 +2.18349022e-03j
  -7.14424927e-04 +1.99668391e-03j  -7.08988969e-04 +1.83798479e-03j
  -7.04625809e-04 +1.70111718e-03j  -7.01067092e-04 +1.58158202e-03j
  -6.98124435e-04 +1.47606010e-03j  -6.95662168e-04 +1.38204336e-03j
  -6.93580412e-04 +1.29759768e-03j  -6.91804223e-04 +1.22120524e-03j
  -6.90276419e-04 +1.15165663e-03j  -6.88952725e-04 +1.08797524e-03j
  -6.87798401e-04 +1.02936305e-03j  -6.86785865e-04 +9.75161003e-04j
  -6.85892989e-04 +9.24819637e-04j  -6.85101840e-04 +8.77876878e-04j
  -6.84397761e-04 +8.33941085e-04j  -6.83768676e-04 +7.92677920e-04j
  -6.83204559e-04 +7.53800070e-04j  -6.82697031e-04 +7.17059116e-04j
  -6.82239044e-04 +6.82239044e-04j  -6.81824635e-04 +6.49151017e-04j
  -6.81448735e-04 +6.17629132e-04j  -6.81107007e-04 +5.87526945e-04j
  -6.80795730e-04 +5.58714616e-04j  -6.80511692e-04 +5.31076537e-04j
  -6.80252114e-04 +5.04509352e-04j  -6.80014579e-04 +4.78920301e-04j
  -6.79796980e-04 +4.54225820e-04j  -6.79597473e-04 +4.30350354e-04j
  -6.79414441e-04 +4.07225345e-04j  -6.79246463e-04 +3.84788368e-04j
  -6.79092283e-04 +3.62982388e-04j  -6.78950795e-04 +3.41755111e-04j
  -6.78821019e-04 +3.21058431e-04j  -6.78702085e-04 +3.00847944e-04j
  -6.78593226e-04 +2.81082517e-04j  -6.78493756e-04 +2.61723923e-04j
  -6.78403070e-04 +2.42736500e-04j  -6.78320630e-04 +2.24086868e-04j
  -6.78245960e-04 +2.05743663e-04j  -6.78178638e-04 +1.87677305e-04j
  -6.78118292e-04 +1.69859790e-04j  -6.78064595e-04 +1.52264497e-04j
  -6.78017263e-04 +1.34866019e-04j  -6.77976048e-04 +1.17640004e-04j
  -6.77940738e-04 +1.00563009e-04j  -6.77911153e-04 +8.36123658e-05j
  -6.77887144e-04 +6.67660561e-05j  -6.77868592e-04 +5.00025914e-05j
  -6.77855406e-04 +3.33009007e-05j  -6.77847520e-04 +1.66402226e-05j
  -6.77844895e-04 +0.00000000e+00j  -6.77847520e-04 -1.66402226e-05j
  -6.77855406e-04 -3.33009007e-05j  -6.77868592e-04 -5.00025914e-05j
  -6.77887144e-04 -6.67660561e-05j  -6.77911153e-04 -8.36123658e-05j
  -6.77940738e-04 -1.00563009e-04j  -6.77976048e-04 -1.17640004e-04j
  -6.78017263e-04 -1.34866019e-04j  -6.78064595e-04 -1.52264497e-04j
  -6.78118292e-04 -1.69859790e-04j  -6.78178638e-04 -1.87677305e-04j
  -6.78245960e-04 -2.05743663e-04j  -6.78320630e-04 -2.24086868e-04j
  -6.78403070e-04 -2.42736500e-04j  -6.78493756e-04 -2.61723923e-04j
  -6.78593226e-04 -2.81082517e-04j  -6.78702085e-04 -3.00847944e-04j
  -6.78821019e-04 -3.21058431e-04j  -6.78950795e-04 -3.41755111e-04j
  -6.79092283e-04 -3.62982388e-04j  -6.79246463e-04 -3.84788368e-04j
  -6.79414441e-04 -4.07225345e-04j  -6.79597473e-04 -4.30350354e-04j
  -6.79796980e-04 -4.54225820e-04j  -6.80014579e-04 -4.78920301e-04j
  -6.80252114e-04 -5.04509352e-04j  -6.80511692e-04 -5.31076537e-04j
  -6.80795730e-04 -5.58714616e-04j  -6.81107007e-04 -5.87526945e-04j
  -6.81448735e-04 -6.17629132e-04j  -6.81824635e-04 -6.49151017e-04j
  -6.82239044e-04 -6.82239044e-04j  -6.82697031e-04 -7.17059116e-04j
  -6.83204559e-04 -7.53800070e-04j  -6.83768676e-04 -7.92677920e-04j
  -6.84397761e-04 -8.33941085e-04j  -6.85101840e-04 -8.77876878e-04j
  -6.85892989e-04 -9.24819637e-04j  -6.86785865e-04 -9.75161003e-04j
  -6.87798401e-04 -1.02936305e-03j  -6.88952725e-04 -1.08797524e-03j
  -6.90276419e-04 -1.15165663e-03j  -6.91804223e-04 -1.22120524e-03j
  -6.93580412e-04 -1.29759768e-03j  -6.95662168e-04 -1.38204336e-03j
  -6.98124435e-04 -1.47606010e-03j  -7.01067092e-04 -1.58158202e-03j
  -7.04625809e-04 -1.70111718e-03j  -7.08988969e-04 -1.83798479e-03j
  -7.14424927e-04 -1.99668391e-03j  -7.21327735e-04 -2.18349022e-03j
  -7.30297537e-04 -2.40746834e-03j  -7.42290393e-04 -2.68229282e-03j
  -7.58918476e-04 -3.02977239e-03j  -7.83110774e-04 -3.48735064e-03j
  -8.20763560e-04 -4.12625706e-03j  -8.85687136e-04 -5.10434074e-03j
  -1.01943025e-03 -6.87244050e-03j  -1.43238147e-03 -1.16134422e-02j
   2.40057698e-02 +2.43734671e-01j   1.71184207e-05 +2.32068767e-04j
   1.22100235e-02 +2.48540738e-01j   6.19890947e-03 +2.52515577e-01j]
Out[11]:
(0, 20)

The FFT results in a complex vector of 128 entries which gives the $a_i$ and $b_i$ coefficients. The vector needs to be divided by half of the number of samples to get the proper amplitudes.

The first entry is the zero-frequency term, corresponding to the mean amplitude of the signal. Entries 1 through 64 are the coefficients for the positive frequencies, while the remaining 64 entries are the coefficients for the negative frequencies. Since our input signal was real, the coefficients of the corresponding positive and negative frequencies are complex conjugates, meaning that we can ignore the coefficients for the negative frequencies.

To compute the amplitudes, we need to take the magnitudes of the complex numbers.

Analysis of CPU Time

To demonstrate the usage of the FFT in applied situation, we'll analyze CPU usage for a single machine over four days. The CPU usage was sampled every 10 min. The collected data was broken into four groups, one per day. We'll use the FFT to look for common frequencies.


In [12]:
def read_cpu_usage(flname):
    fl = open(flname)
    cpu_usage = []
    for ln in fl:
        time_s, usage_s = ln.split()
        cpu_usage.append((time_s, float(usage_s)))
    fl.close()
    return zip(*cpu_usage)

sample_period = 10.

machine1_01 = np.array(read_cpu_usage("fft_usage_data/machine1_01_cpu.txt")[1])
machine1_02 = np.array(read_cpu_usage("fft_usage_data/machine1_02_cpu.txt")[1])
machine1_03 = np.array(read_cpu_usage("fft_usage_data/machine1_03_cpu.txt")[1])
machine1_25 = np.array(read_cpu_usage("fft_usage_data/machine1_25_cpu.txt")[1])

elapsed_time_min = 24. * 60.
sample_times = np.arange(sample_period, elapsed_time_min, sample_period)
plt.plot(sample_times, machine1_01)
plt.plot(sample_times, machine1_02)
plt.plot(sample_times, machine1_03)
plt.plot(sample_times, machine1_25)
plt.xlabel("Time (minutes)", fontsize=16)
plt.ylabel("CPU Usage(%)", fontsize=16)
plt.ylim([0.0, 20.0])


Out[12]:
(0.0, 20.0)

I plotted the CPU usage for the machine on the four days. Note that the four signals overlap nearly identifically for a subset of higher frequency perturbations. The low frequency low perturbations are quite a bit different. Using a FT, we can extract the frequencies to identify which signals overlap.

By the Nyquist theorem, our fastest recoverable signal will have a period of 20 min. Our slowest signal will have a period of 24 hours.


In [13]:
n_samples = 143
freq = np.fft.fftfreq(n_samples, d=sample_period)

print freq

coeff1_01 = np.fft.fft(machine1_01) / (n_samples / 2.0)
amplitudes1_01 = np.abs(coeff1_01)

coeff1_02 = np.fft.fft(machine1_02) / (n_samples / 2.0)
amplitudes1_02 = np.abs(coeff1_02)

coeff1_03 = np.fft.fft(machine1_03) / (n_samples / 2.0)
amplitudes1_03 = np.abs(coeff1_03)

coeff1_25 = np.fft.fft(machine1_25) / (n_samples / 2.0)
amplitudes1_25 = np.abs(coeff1_25)

plt.plot(freq[:(n_samples+1)/2], amplitudes1_01[:(n_samples+1)/2])
plt.plot(freq[:(n_samples+1)/2], amplitudes1_02[:(n_samples+1)/2])
plt.plot(freq[:(n_samples+1)/2], amplitudes1_03[:(n_samples+1)/2])
plt.plot(freq[:(n_samples+1)/2], amplitudes1_25[:(n_samples+1)/2])
plt.plot([0.015, 0.015], [0, 3.5], "k-")
plt.plot([0.019, 0.019], [0, 3.5], "k-")
plt.plot([0.03, 0.03], [0, 3.5], "k-")
plt.plot([0.0375, 0.0375], [0, 3.5], "k-")
plt.xlabel("Frequency (cycles/min)", fontsize=16)
plt.ylabel("Amplitude", fontsize=16)
plt.xlim([0, max(freq)])


[ 0.          0.0006993   0.0013986   0.0020979   0.0027972   0.0034965
  0.0041958   0.0048951   0.00559441  0.00629371  0.00699301  0.00769231
  0.00839161  0.00909091  0.00979021  0.01048951  0.01118881  0.01188811
  0.01258741  0.01328671  0.01398601  0.01468531  0.01538462  0.01608392
  0.01678322  0.01748252  0.01818182  0.01888112  0.01958042  0.02027972
  0.02097902  0.02167832  0.02237762  0.02307692  0.02377622  0.02447552
  0.02517483  0.02587413  0.02657343  0.02727273  0.02797203  0.02867133
  0.02937063  0.03006993  0.03076923  0.03146853  0.03216783  0.03286713
  0.03356643  0.03426573  0.03496503  0.03566434  0.03636364  0.03706294
  0.03776224  0.03846154  0.03916084  0.03986014  0.04055944  0.04125874
  0.04195804  0.04265734  0.04335664  0.04405594  0.04475524  0.04545455
  0.04615385  0.04685315  0.04755245  0.04825175  0.04895105  0.04965035
 -0.04965035 -0.04895105 -0.04825175 -0.04755245 -0.04685315 -0.04615385
 -0.04545455 -0.04475524 -0.04405594 -0.04335664 -0.04265734 -0.04195804
 -0.04125874 -0.04055944 -0.03986014 -0.03916084 -0.03846154 -0.03776224
 -0.03706294 -0.03636364 -0.03566434 -0.03496503 -0.03426573 -0.03356643
 -0.03286713 -0.03216783 -0.03146853 -0.03076923 -0.03006993 -0.02937063
 -0.02867133 -0.02797203 -0.02727273 -0.02657343 -0.02587413 -0.02517483
 -0.02447552 -0.02377622 -0.02307692 -0.02237762 -0.02167832 -0.02097902
 -0.02027972 -0.01958042 -0.01888112 -0.01818182 -0.01748252 -0.01678322
 -0.01608392 -0.01538462 -0.01468531 -0.01398601 -0.01328671 -0.01258741
 -0.01188811 -0.01118881 -0.01048951 -0.00979021 -0.00909091 -0.00839161
 -0.00769231 -0.00699301 -0.00629371 -0.00559441 -0.0048951  -0.0041958
 -0.0034965  -0.0027972  -0.0020979  -0.0013986  -0.0006993 ]
Out[13]:
(0, 0.049650349650349652)

The frequency analysis show the highest-levels of similarity in the ranges of 0.015 - 0.019 cycles/min and 0.03 - 0.0375 cycles/min, demarcated by the black horizontal lines. These frequencies correspond to periods of approximately 53 - 67 min and 2-5 - 3.5 min, respectively. Overall, the signals show qualitatively similarly with frequencies above 0.015 cycles/min or periods below 67 min.

Let's look at a second machine. In this case, the data was sampled every 5 min over three days.


In [14]:
sample_period = 5.

machine2_01 = np.array(read_cpu_usage("fft_usage_data/machine2_01_cpu.txt")[1])
machine2_02 = np.array(read_cpu_usage("fft_usage_data/machine2_02_cpu.txt")[1])
machine2_03 = np.array(read_cpu_usage("fft_usage_data/machine2_03_cpu.txt")[1])

elapsed_time_min = 24. * 60.
sample_times = np.arange(sample_period, elapsed_time_min - sample_period, sample_period)
plt.plot(sample_times, machine2_01)
plt.plot(sample_times, machine2_02)
plt.plot(sample_times, machine2_03)
plt.xlabel("Time (minutes)", fontsize=16)
plt.ylabel("CPU Usage(%)", fontsize=16)
plt.ylim([0.0, 100.0])


Out[14]:
(0.0, 100.0)

I plotted the CPU usage for the machine on the three days. Note that the three signals overlap nearly identifically -- they are more similary than those of the first machine. Again, we'll use a FT to extract the frequencies to identify which signals overlap.

By the Nyquist theorem, our fastest recoverable signal will have a period of 10 min. Our slowest signal will have a period of 24 hours.


In [15]:
sample_period = 5.
n_samples = 286
freq = np.fft.fftfreq(n_samples, d=sample_period)

print freq

coeff2_01 = np.fft.fft(machine2_01) / (n_samples / 2.0)
amplitudes2_01 = np.abs(coeff2_01)

coeff2_02 = np.fft.fft(machine2_02) / (n_samples / 2.0)
amplitudes2_02 = np.abs(coeff2_02)

coeff2_03 = np.fft.fft(machine2_03) / (n_samples / 2.0)
amplitudes2_03 = np.abs(coeff2_03)

plt.plot(freq[:(n_samples+1)/2], amplitudes2_01[:(n_samples+1)/2])
plt.plot(freq[:(n_samples+1)/2], amplitudes2_02[:(n_samples+1)/2])
plt.plot(freq[:(n_samples+1)/2], amplitudes2_03[:(n_samples+1)/2])
plt.xlabel("Frequency (cycles/min)", fontsize=16)
plt.ylabel("Amplitude", fontsize=16)
plt.xlim([0, max(freq)])


[ 0.          0.0006993   0.0013986   0.0020979   0.0027972   0.0034965
  0.0041958   0.0048951   0.00559441  0.00629371  0.00699301  0.00769231
  0.00839161  0.00909091  0.00979021  0.01048951  0.01118881  0.01188811
  0.01258741  0.01328671  0.01398601  0.01468531  0.01538462  0.01608392
  0.01678322  0.01748252  0.01818182  0.01888112  0.01958042  0.02027972
  0.02097902  0.02167832  0.02237762  0.02307692  0.02377622  0.02447552
  0.02517483  0.02587413  0.02657343  0.02727273  0.02797203  0.02867133
  0.02937063  0.03006993  0.03076923  0.03146853  0.03216783  0.03286713
  0.03356643  0.03426573  0.03496503  0.03566434  0.03636364  0.03706294
  0.03776224  0.03846154  0.03916084  0.03986014  0.04055944  0.04125874
  0.04195804  0.04265734  0.04335664  0.04405594  0.04475524  0.04545455
  0.04615385  0.04685315  0.04755245  0.04825175  0.04895105  0.04965035
  0.05034965  0.05104895  0.05174825  0.05244755  0.05314685  0.05384615
  0.05454545  0.05524476  0.05594406  0.05664336  0.05734266  0.05804196
  0.05874126  0.05944056  0.06013986  0.06083916  0.06153846  0.06223776
  0.06293706  0.06363636  0.06433566  0.06503497  0.06573427  0.06643357
  0.06713287  0.06783217  0.06853147  0.06923077  0.06993007  0.07062937
  0.07132867  0.07202797  0.07272727  0.07342657  0.07412587  0.07482517
  0.07552448  0.07622378  0.07692308  0.07762238  0.07832168  0.07902098
  0.07972028  0.08041958  0.08111888  0.08181818  0.08251748  0.08321678
  0.08391608  0.08461538  0.08531469  0.08601399  0.08671329  0.08741259
  0.08811189  0.08881119  0.08951049  0.09020979  0.09090909  0.09160839
  0.09230769  0.09300699  0.09370629  0.09440559  0.0951049   0.0958042
  0.0965035   0.0972028   0.0979021   0.0986014   0.0993007  -0.1
 -0.0993007  -0.0986014  -0.0979021  -0.0972028  -0.0965035  -0.0958042
 -0.0951049  -0.09440559 -0.09370629 -0.09300699 -0.09230769 -0.09160839
 -0.09090909 -0.09020979 -0.08951049 -0.08881119 -0.08811189 -0.08741259
 -0.08671329 -0.08601399 -0.08531469 -0.08461538 -0.08391608 -0.08321678
 -0.08251748 -0.08181818 -0.08111888 -0.08041958 -0.07972028 -0.07902098
 -0.07832168 -0.07762238 -0.07692308 -0.07622378 -0.07552448 -0.07482517
 -0.07412587 -0.07342657 -0.07272727 -0.07202797 -0.07132867 -0.07062937
 -0.06993007 -0.06923077 -0.06853147 -0.06783217 -0.06713287 -0.06643357
 -0.06573427 -0.06503497 -0.06433566 -0.06363636 -0.06293706 -0.06223776
 -0.06153846 -0.06083916 -0.06013986 -0.05944056 -0.05874126 -0.05804196
 -0.05734266 -0.05664336 -0.05594406 -0.05524476 -0.05454545 -0.05384615
 -0.05314685 -0.05244755 -0.05174825 -0.05104895 -0.05034965 -0.04965035
 -0.04895105 -0.04825175 -0.04755245 -0.04685315 -0.04615385 -0.04545455
 -0.04475524 -0.04405594 -0.04335664 -0.04265734 -0.04195804 -0.04125874
 -0.04055944 -0.03986014 -0.03916084 -0.03846154 -0.03776224 -0.03706294
 -0.03636364 -0.03566434 -0.03496503 -0.03426573 -0.03356643 -0.03286713
 -0.03216783 -0.03146853 -0.03076923 -0.03006993 -0.02937063 -0.02867133
 -0.02797203 -0.02727273 -0.02657343 -0.02587413 -0.02517483 -0.02447552
 -0.02377622 -0.02307692 -0.02237762 -0.02167832 -0.02097902 -0.02027972
 -0.01958042 -0.01888112 -0.01818182 -0.01748252 -0.01678322 -0.01608392
 -0.01538462 -0.01468531 -0.01398601 -0.01328671 -0.01258741 -0.01188811
 -0.01118881 -0.01048951 -0.00979021 -0.00909091 -0.00839161 -0.00769231
 -0.00699301 -0.00629371 -0.00559441 -0.0048951  -0.0041958  -0.0034965
 -0.0027972  -0.0020979  -0.0013986  -0.0006993 ]
Out[15]:
(0, 0.099300699300699305)

As with the CPU usage plots, the frequency plots for the second machine are nearly identical at all frequencies.

What if we compare the two machines?


In [16]:
elapsed_time_min = 24. * 60.
plt.plot(np.arange(10., elapsed_time_min, 10.), machine1_01, label="M1")
plt.plot(np.arange(5., elapsed_time_min - 5., 5.), machine2_01, label="M2")
plt.xlabel("Time (minutes)", fontsize=16)
plt.ylabel("CPU Usage(%)", fontsize=16)
plt.ylim([0.0, 100.0])
plt.legend()


Out[16]:
<matplotlib.legend.Legend at 0x46fd8d0>

In [21]:
freq1 = np.fft.fftfreq(143, d=10.)
freq2 = np.fft.fftfreq(286, d=5.)

coeff1_01 = np.fft.fft(machine1_01) / (143. / 2.0)
amplitudes1_01 = np.abs(coeff1_01)

coeff2_01 = np.fft.fft(machine2_01) / (286. / 2.0)
amplitudes2_01 = np.abs(coeff2_01)

# since machine2 has more samples, the FFT can resolve twice as many
# high frequencies vs machine1. we have to use remove half the spectrum
# to make them comparable
plt.plot(freq1[:143/2], amplitudes1_01[:143/2], label="M1")
plt.plot(freq2[:143/2], amplitudes2_01[:143/2], label="M2")
plt.xlabel("Frequency (cycles/min)", fontsize=16)
plt.ylabel("Amplitude", fontsize=16)
plt.xlim([0, max(freq1)])
plt.legend()


Out[21]:
<matplotlib.legend.Legend at 0x55c7f90>

The FFTs of the usage from the two machines varies quite a bit qualitatively. First off, the amplitudes are very different due to the heavy cpu usage of machine 2. Machine 2 also had signals present across a wider range of frequencies.


In [17]: